Skip to content

perf: Density states in DuckDB's arena; take the library from the Query.Farm fork - #34

Merged
rustyconover merged 1 commit into
v2.0from
feat/density-arena
Oct 28, 2026
Merged

rustyconover merged 1 commit into
v2.0from
feat/density-arena

Conversation

@rustyconover

Copy link
Copy Markdown
Collaborator

Density was the last sketch family whose aggregate states stayed on the heap. The library's kernel only accepted std::vector<double>, so the sketch couldn't use our allocator.

20M points, 2M groups, 8 threads before after
1 GB limit 3.9 GB peak (limit ignored), 10.1 s 1.1 GB, 7.6 s
4 GB limit 3.8 GB, 10.4 s 2.1 GB, 3.2 s

Library from the fork

vcpkg-overlay/apache-datasketches now points vcpkg_from_github at rustyconover/datasketches-cpp, branch duckdb-5.3.0 (44f777b). That branch is release 5.3.0 plus one commit per fix we've sent upstream, and it replaces the overlay's 8 patch files:

Upstream Fix
apache/datasketches-cpp#552 REQ kept K in 8 bits
#555 (issue) Count-Min hash seeds differed between standard libraries
#556 HLL union copied inputs with their allocator
#557 EBPPS merge(const&) called an unqualified swap
#558 KLL allocated its items in the constructor
#559 Count-Min hash seeds ignored the sketch's allocator
#560 Frequent-items purge allocated its sample each time
#561 TDigest reserved its buffers up front
#562 (new) Density didn't work with a custom allocator: the kernel only took std::vector<T>, and an unused allocator member was always default-constructed

The fork branch passes all 17 library test suites. When an upstream release includes a fix, its commit can be dropped and the REF moved.

Density state

Density aggregates now keep their sketches, points included, in the aggregate arena, and export and import them. A point copies with its own allocator, so:

  • input sketches are read into a scratch arena, then merged while the state's arena is current, so the copies land there;
  • new points are built in the arena directly;
  • serialize and export use the existing scratch-bytes helper.

Tests

New test/sql/datasketch_spill_density.test, under 128 MB with 4 threads:

  • 300k exact groups, compared against the same query at 4 GB
  • merges of stored sketches
  • estimation-mode merges
  • EXPORT_STATE round trips

All tests pass (37 test files, 2165 assertions).

🤖 Generated with Claude Code

…ry.Farm fork

The density sketch's kernel only accepted std::vector<double>, so the sketch
could not use DSAllocator and its aggregate states stayed on the heap, out of
reach of memory_limit (3.9 GB under a 1 GB limit for 2M groups). The library
now comes from the fork's duckdb-5.3.0 branch: release 5.3.0 plus one commit
per fix sent upstream (apache/datasketches-cpp#552, #555-#562), replacing the
overlay's patch files. #562 makes the density kernel take any vector.

Density aggregates keep their sketches, points included, in the aggregate
arena with export and import. Points copy with their own allocator, so input
sketches are read into a scratch arena and merged while the state's arena is
current. The same query now runs in 1.1 GB under 1 GB (7.6 s, from 10.1 s)
and in 3.2 s under 4 GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rustyconover
rustyconover merged commit 7ba6198 into v2.0 Oct 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant